﻿using System;
using System.Collections.Generic;
using System.Linq;
using DAL.Models;

namespace DAL.DBManagers
{
    public class VesselsManager : BaseManager<Vessel>
    {
        public VesselsManager()
            : base()
        {
        }

        public override IEnumerable<Vessel> GetAll()
        {
            return (_context as PortEntities).Vessels.Include("VesselType").Include("Flag").Where(w => w.IsActive).ToList();
        }

        public override List<Vessel> GetAll(string sortBy)
        {
            List<Vessel> prodList = (_context as PortEntities).Vessels.Include("VesselType").Include("Flag").Where(w => w.IsActive).ToList();
            if (sortBy != "")
            {
                DAL.Models.GenericComparer<Vessel> cmp = new DAL.Models.GenericComparer<Vessel>(sortBy);
                prodList.Sort(cmp);
            }
            return prodList;
        }

        public string GetVesselCodeID(Guid vesselid, bool linerstatus, Guid purposeid)
        {
            var objparam1 = new System.Data.Objects.ObjectParameter("VesselCodeID", typeof(Guid));
            var objparam2 = new System.Data.Objects.ObjectParameter("VesselCode", typeof(string));
            var result = (_context as PortEntities).GetVesselCode(vesselid, linerstatus, purposeid, objparam1, objparam2);

            return objparam2.Value.ToString();
        }

        public List<VesselHelper> GetVessels()
        {
            var context = (PortEntities)this._context;

            return (from f in context.Vessels
                    join n in context.Flags on f.FlagID equals n.FlagID
                    join t in context.VesselTypes on f.VesselTypeID equals t.VesselTypeID
                    where f.IsActive && n.IsActive && t.IsActive &&
                          f.FinishDate == null && n.FinishDate == null && t.FinishDate == null
                    select new VesselHelper
                    {
                        RecordID = f.RecordID,
                        ID = f.VesselID,
                        Name = f.Name,
                        NameGeo = f.NameGeo,
                        BeginDate = f.BeginDate,
                        FinishDate = f.FinishDate,
                        IMO = f.IMO,
                        Depth = f.Depth,
                        Beam = f.Beam,
                        LOA = f.LOA,
                        SBT = f.SBT,
                        Gross = f.Gross,
                        Deadweight = f.Deadweight,
                        Draft = f.Draft,
                        Flag = new FlagHelper
                        {
                            RecordID = n.RecordID,
                            ID = n.FlagID,
                            Name = n.Name,
                            NameGeo = n.NameGeo,
                            BeginDate = n.BeginDate,
                            FinishDate = n.FinishDate,
                            IsActive = n.IsActive
                        },
                        VesselType = new VesselTypeHelper
                        {
                            RecordID = t.RecordID,
                            ID = t.VesselTypeID,
                            Name = t.Name,
                            NameGeo = t.NameGeo,
                            BeginDate = t.BeginDate,
                            FinishDate = t.FinishDate,
                            IsActive = t.IsActive
                        },
                        IsActive = f.IsActive,
                        CreateDate = f.CreateDate
                    }).OrderBy(o => o.Name).ToList();
        }

        public List<VesselHelper> GetVessels(string sortBy)
        {
            var context = (PortEntities)this._context;
            List<VesselHelper> prodList = (from f in context.Vessels
                                           join n in context.Flags on f.FlagID equals n.FlagID
                                           join t in context.VesselTypes on f.VesselTypeID equals t.VesselTypeID
                                           where f.IsActive && n.IsActive && t.IsActive &&
                                                 f.FinishDate == null && n.FinishDate == null && t.FinishDate == null
                                           select new VesselHelper
                                           {
                                               RecordID = f.RecordID,
                                               ID = f.VesselID,
                                               Name = f.Name,
                                               NameGeo = f.NameGeo,
                                               BeginDate = f.BeginDate,
                                               FinishDate = f.FinishDate,
                                               IMO = f.IMO,
                                               Depth = f.Depth,
                                               Beam = f.Beam,
                                               LOA = f.LOA,
                                               SBT = f.SBT,
                                               Gross = f.Gross,
                                               Deadweight = f.Deadweight,
                                               Draft = f.Draft,
                                               Flag = new FlagHelper
                                               {
                                                   RecordID = n.RecordID,
                                                   ID = n.FlagID,
                                                   Name = n.Name,
                                                   NameGeo = n.NameGeo,
                                                   BeginDate = n.BeginDate,
                                                   FinishDate = n.FinishDate,
                                                   IsActive = n.IsActive
                                               },
                                               VesselType = new VesselTypeHelper
                                               {
                                                   RecordID = t.RecordID,
                                                   ID = t.VesselTypeID,
                                                   Name = t.Name,
                                                   NameGeo = t.NameGeo,
                                                   BeginDate = t.BeginDate,
                                                   FinishDate = t.FinishDate,
                                                   IsActive = t.IsActive
                                               },
                                               IsActive = f.IsActive,
                                               CreateDate = f.CreateDate
                                           }).OrderBy(o => o.Name).ToList();
            if (sortBy != "")
            {
                DAL.Models.GenericComparer<VesselHelper> cmp = new DAL.Models.GenericComparer<VesselHelper>(sortBy);
                prodList.Sort(cmp);
            }
            return prodList;
        }

        public List<VesselHelper> GetVessels(Guid VesselID)
        {
            var context = (PortEntities)this._context;

            return (from f in context.Vessels
                    join n in context.Flags on f.FlagID equals n.FlagID
                    join t in context.VesselTypes on f.VesselTypeID equals t.VesselTypeID
                    where f.IsActive && n.IsActive && t.IsActive &&
                    f.VesselID == VesselID
                                            &&
                    n.RecordID == context.Flags.Where(w => w.IsActive && w.FlagID == n.FlagID && w.BeginDate <= f.BeginDate)
                                               .OrderByDescending(o => o.BeginDate).FirstOrDefault().RecordID
                                               &&
                    t.RecordID == context.VesselTypes.Where(w => w.IsActive && w.VesselTypeID == t.VesselTypeID && w.BeginDate <= f.BeginDate)
                    .OrderByDescending(o => o.BeginDate).FirstOrDefault().RecordID
                    select new VesselHelper
                    {
                        RecordID = f.RecordID,
                        ID = f.VesselID,
                        Name = f.Name,
                        NameGeo = f.NameGeo,
                        BeginDate = f.BeginDate,
                        FinishDate = f.FinishDate,
                        IMO = f.IMO,
                        Depth = f.Depth,
                        Beam = f.Beam,
                        LOA = f.LOA,
                        SBT = f.SBT,
                        Gross = f.Gross,
                        Deadweight = f.Deadweight,
                        Draft = f.Draft,
                        Flag = new FlagHelper
                        {
                            RecordID = n.RecordID,
                            ID = n.FlagID,
                            Name = n.Name,
                            NameGeo = n.NameGeo,
                            BeginDate = n.BeginDate,
                            FinishDate = n.FinishDate,
                            IsActive = n.IsActive
                        },
                        VesselType = new VesselTypeHelper
                        {
                            RecordID = t.RecordID,
                            ID = t.VesselTypeID,
                            Name = t.Name,
                            NameGeo = t.NameGeo,
                            BeginDate = t.BeginDate,
                            FinishDate = t.FinishDate,
                            IsActive = t.IsActive
                        },
                        IsActive = f.IsActive
                    }).OrderByDescending(o => o.BeginDate).ToList();
        }

        public List<VesselHelper> GetVessels(Guid VesselID, string sortBy)
        {
            var context = (PortEntities)this._context;

            var prodList = (from f in context.Vessels
                            join n in context.Flags on f.FlagID equals n.FlagID
                            join t in context.VesselTypes on f.VesselTypeID equals t.VesselTypeID
                            where f.IsActive && n.IsActive && t.IsActive &&
                            f.VesselID == VesselID
                                                    &&
                            n.RecordID == context.Flags.Where(w => w.IsActive && w.FlagID == n.FlagID && w.BeginDate <= f.BeginDate)
                                                       .OrderByDescending(o => o.BeginDate).FirstOrDefault().RecordID
                                                       &&
                            t.RecordID == context.VesselTypes.Where(w => w.IsActive && w.VesselTypeID == t.VesselTypeID && w.BeginDate <= f.BeginDate)
                            .OrderByDescending(o => o.BeginDate).FirstOrDefault().RecordID
                            select new VesselHelper
                            {
                                RecordID = f.RecordID,
                                ID = f.VesselID,
                                Name = f.Name,
                                NameGeo = f.NameGeo,
                                BeginDate = f.BeginDate,
                                FinishDate = f.FinishDate,
                                IMO = f.IMO,
                                Depth = f.Depth,
                                Beam = f.Beam,
                                LOA = f.LOA,
                                SBT = f.SBT,
                                Gross = f.Gross,
                                Deadweight = f.Deadweight,
                                Draft = f.Draft,
                                Flag = new FlagHelper
                                {
                                    RecordID = n.RecordID,
                                    ID = n.FlagID,
                                    Name = n.Name,
                                    NameGeo = n.NameGeo,
                                    BeginDate = n.BeginDate,
                                    FinishDate = n.FinishDate,
                                    IsActive = n.IsActive
                                },
                                VesselType = new VesselTypeHelper
                                {
                                    RecordID = t.RecordID,
                                    ID = t.VesselTypeID,
                                    Name = t.Name,
                                    NameGeo = t.NameGeo,
                                    BeginDate = t.BeginDate,
                                    FinishDate = t.FinishDate,
                                    IsActive = t.IsActive
                                },
                                IsActive = f.IsActive
                            }).OrderByDescending(o => o.BeginDate).ToList();
            if (sortBy != "")
            {
                DAL.Models.GenericComparer<VesselHelper> cmp = new DAL.Models.GenericComparer<VesselHelper>(sortBy);
                prodList.Sort(cmp);
            }
            return prodList;
        }

        public void RepiarDates(Guid VesselID)
        {
            var vessels = this.Find(f => f.IsActive && f.VesselID == VesselID).OrderBy(o => o.BeginDate).ToList();

            var count = vessels.Count();

            for (int i = 0; i < count; i++)
            {
                if (i < count - 1)
                {
                    if (vessels[i].FinishDate != vessels[i + 1].BeginDate)
                    {
                        vessels[i].FinishDate = vessels[i + 1].BeginDate;
                    }
                }
                else if (i == count - 1)
                {
                    if (vessels[i].FinishDate != null)
                    {
                        vessels[i].FinishDate = null;
                    }
                }
            }

            SaveChanges();
        }
    }
}
